<?php

namespace app\services\tencentCloud\ocr\ocr;

class OcrMatch
{
    public string $money; //金额
    public $pay_date; //支付日期
    public $payee; //收款人
    public $payer; //付款人
    public $due_bank; //收款行
    public $paying_bank; //付款行
    public $due_bank_number;  //收款账号
    public $paying_bank_number;  //付款账号

    public $db_data;
    public $ocr_data;

    public function get($db_data,$ocr_data)
    {
        $this->db_data = $db_data;
        $this->ocr_data = $ocr_data;
        return [
            'money' => $this->getMoney(),
            'pay_date' => $this->getPayDate(),
            'payee' => $this->getPayee(),
            'payer' => $this->getPayer(),
            'due_bank' => $this->getDueBank(),
            'paying_bank' => $this->getPayingBank(),
            'due_bank_number' => $this->getDueBankNumber(),
            'paying_bank_number' => $this->getPayingBankNumber(),
            'ocr_data'=>implode("|",$this->ocr_data),
            'db_data'=>$this->db_data,
        ];
    }

    protected function getPayDate(){

        $date_pattern = $preg = '/[2][0][2]\d{1}\-\d{1,2}\-\d{1,2}|[2][0][2]\d{1}\/\d{1,2}\/\d{1,2}|[2][0][2]\d{1}年\d{1,2}月\d{1,2}日|[2][0][2]\d{1}\.\d{1,2}\.\d{1,2}|^[2][0][2]\d{1}\d{1,2}\d{1,2}/';
        $matches_date = $this->pregMatch($date_pattern,$this->ocr_data);
        if(!empty($matches_date)) {
            $match_date = $matches_date[0];
            if (strpos($match_date, '年') || strpos($match_date, '月') || strpos($match_date, '日')) {
                $this->pay_date = mb_substr($match_date,0,4).'-'.mb_substr($match_date,5,2).'-'.mb_substr($match_date,8,2);
            }elseif(strpos($match_date, '.')){
                $date = str_replace('.','-',$match_date);
                $this->pay_date = date('Y-m-d', strtotime($date));
            }else{
                $this->pay_date = date('Y-m-d', strtotime($match_date));
            }
        }
        return $this->pay_date;
    }
    protected function getMoney(){
        //匹配汇款金额
        $money_pattern = '/[0-9]{0,3}\,?\.?[0-9]{0,3}\,?\.?000\.00|[0-9]{0,3}\,?\.?[0-9]{0,3}\,?\.?000元|[0-9]{0,3}\,?\.?[0-9]{0,3}\,?\.?000\.0/';
        $matches_money = $this->pregMatch($money_pattern,$this->ocr_data);

        if(!empty($matches_money)) {
            $match_money = $matches_money[0];
            $last_word = substr($match_money, strrpos($match_money, '.'));
            $this->money = OcrMatch . phpstr_replace('.', '', str_replace(',', '', substr($match_money, 0, strlen($match_money) - 3))) . $last_word;
        }
        return $this->money;
    }
    protected function getPayee(){
        $result= $this->similarTextMatch($this->db_data['payee']??null,$this->ocr_data);
        if(!$result){return false;}
        $this->payee=$result[0];
        return $this->payee;
    }
    protected function getPayer(){
        $result= $this->similarTextMatch($this->db_data['payer']??null,$this->ocr_data);
        if(!$result){return false;}
        $this->payer=$result[0];
        return $this->payer;
    }
    protected function getDueBank(){
        $result= $this->similarTextMatch($this->db_data['due_bank']??null,$this->ocr_data);
        if(!$result){return false;}
        $this->due_bank=$result[0];
        return $this->due_bank;
    }
    protected function getPayingBank(){
        $result= $this->similarTextMatch($this->db_data['paying_bank']??null,$this->ocr_data);
        if(!$result){return false;}
        $this->paying_bank=$result[0];
        return $this->paying_bank;
    }
    protected function getDueBankNumber(){
        $result= $this->similarTextMatch($this->db_data['due_bank_number']??null,$this->ocr_data);
        if(!$result){return false;}
        $this->due_bank_number=$result[0];
        return $this->due_bank_number;
    }
    protected function getPayingBankNumber(){
        $result= $this->similarTextMatch($this->db_data['paying_bank_number']??null,$this->ocr_data);
        if(!$result){return false;}
        $this->paying_bank_number=$result[0];
        return $this->paying_bank_number;
    }

    private function similarTextMatch($text,$array)
    {
        if(!$text){
            return false;
        }
        $match_rate = 0;
        $value = '';
        $index = null;
        foreach($array as $k=>$v)
        {
            similar_text($text,$v,$rate);
            if($rate > $match_rate ){
                $value = $v;
                $match_rate= $rate;
                $index = $k;
            }
        }
        return [$value,$match_rate,$index];
    }

    private function pregMatch($preg,$array)
    {
        $matches=[];
        foreach ($array as $k=>$v) {
            $v = str_replace('，',',',$v);
            preg_match($preg, $v, $matche);
            if(!empty($matche)){
                $matches=$matche;
            }

        }
        return $matches;

    }

}
